﻿using System;
using System.IO;
using System.Reflection;
using Styx.Plugins;

namespace ManagerPlugin
{
    class Loader : HBPlugin
    {
        private static Assembly botHavenAssembly;
        private static HBPlugin botHavenInstance;
        private static bool _initialized;
        public static string HBFolder
        {
            get { return System.Windows.Forms.Application.ExecutablePath; }
        }
        private string PluginFolder
        {
            get { return Path.Combine(Path.GetDirectoryName(HBFolder), "Plugins", "botHavenManager"); }
        }
        public new bool Equals(HBPlugin other)
        {
            return botHavenInstance.Equals(other);
        }

        public override void Initialize()
        {
            botHavenInstance.Initialize();
        }

        public override void Pulse()
        {
            botHavenInstance.Pulse();
        }
        private static void MakeConfigFile(string exeLocation)
        {
            var location = exeLocation + ".config";
            if (File.Exists(location))
                return;
            var content = "<configuration>\n   <runtime>\n      <loadFromRemoteSources enabled=\"true\"/>\n   </runtime>\n</configuration>";
            StreamWriter file = new StreamWriter(location);
            file.WriteLine(content);
            file.Flush();
            file.Close();
        }
        public override string Name
        {
            get
            {
                if (!_initialized)
                {
                    MakeConfigFile(HBFolder);
                    var tmpFiles = Directory.GetFiles(PluginFolder, "*.tmp");
                    foreach (var file in tmpFiles)
                    {
                        try
                        {
                            File.Delete(file);
                        }
                        catch (Exception ex)
                        {

                        }
                    }
                    var dllFile = Path.Combine(Path.GetDirectoryName(HBFolder), "BothavenManager.dll");
                    if (File.Exists(dllFile + ".tmp"))
                        try
                        {
                            File.Delete(dllFile + ".tmp");
                        }
                        catch (Exception ex)
                        {
                            
                        }
                    if (File.Exists(dllFile + ".new"))
                    {
                        try
                        {
                            if (File.Exists(dllFile))
                            {
                                File.Move(dllFile, dllFile + ".tmp");
                            }
                            File.Move(dllFile + ".new", dllFile);
                        }
                        catch (Exception ex)
                        {
                            
                        }
                    }
                    try
                    {
                        if (File.Exists(dllFile + ".tmp"))
                            File.Delete(dllFile + ".tmp");
                    }
                    catch (Exception ex)
                    {
                        
                    }
                    // update
                    object[] args = { HBFolder, Assembly.GetEntryAssembly().GetName().Version.ToString() };
                    Assembly updaterAssembly = Assembly.LoadFile(Path.Combine(PluginFolder, "ManagerUpdater.dll"));
                    updaterAssembly.CreateInstance("ManagerUpdater.Updater", false, BindingFlags.CreateInstance, null, args, null, null);
                    // load manager
                    var buffer = File.ReadAllBytes(Path.Combine(PluginFolder, "ManagerPlugin.dll"));
                    botHavenAssembly = Assembly.Load(buffer);
                    botHavenInstance = (HBPlugin)botHavenAssembly.CreateInstance("ManagerPlugin.Plugin");
                    _initialized = true;
                }
                return botHavenInstance.Name;
            }
        }
        public override string Author
        {
            get { return botHavenInstance.Author; }
        }

        public override Version Version
        {
            get { return botHavenInstance.Version; }
        }
        public override string ButtonText
        {
            get
            {
                return botHavenInstance.ButtonText;
            }
        }
        public override void OnButtonPress()
        {
            botHavenInstance.OnButtonPress();
        }
        public override bool WantButton
        {
            get
            {
                return botHavenInstance.WantButton;
            }
        }

    }
}
